//定义js命名空间
Ext.namespace('Infrastructure.InfraApproveStep');
//DateTime -- date , decimal -- float
// { name: 'Department.Name', mapping: 'Department.Name' },
// { name: 'Department'},
//InfraApproveStep FieldArray
Infrastructure.InfraApproveStep.BaseFieldsArray = new Array(
	{ name: 'Id', type: 'string' },
	{ name: 'ApproveId', type: 'string' },
	{ name: 'ApproveStep', type: 'int' },
	{ name: 'UserId', type: 'string' },
    { name: 'User.Name', mapping: 'User.Name' },
	{ name: 'Scale', type: 'int' },
    { name: 'SmsStartTime', type: 'int' },
    { name: 'SmsWarnTime', type: 'int' },
    { name: 'Scale', type: 'int' },
	{ name: 'Isdelete', type: 'bool' },
	{ name: 'Description', type: 'string' },
	{ name: 'Version', type: 'int' },
	{ name: 'CreateTime', type: 'date' }
);

//InfraApproveStep JsonReader
Infrastructure.InfraApproveStep.JsonReader = Ext.extend(Infrastructure.BaseJsonReader, {
    constructor: function (cfg) {
        cfg = cfg || {};
        Infrastructure.InfraApproveStep.JsonReader.superclass.constructor.call(this, Ext.apply({
            fields: Infrastructure.InfraApproveStep.BaseFieldsArray
        }, cfg));
    }
});

//InfraApproveStep JsonStore
Infrastructure.InfraApproveStep.JsonStore = Ext.extend(Infrastructure.BaseJsonStore, {
    constructor: function (cfg) {
        cfg = cfg || {};
        Infrastructure.InfraApproveStep.JsonStore.superclass.constructor.call(this, Ext.apply({
            url: '/InfraApproveStep/GetPlist',
            params: {
                start: 0,
                limit: ALL_PAGESIZE_SETTING
            },
            sortInfo: {
                field: 'ApproveStep',
                direction: 'ASC' // or 'DESC' (case sensitive for local sorting)
            },
            remoteSort: true,
            fields: Infrastructure.InfraApproveStep.BaseFieldsArray,
            listeners: {
                'remove': function (thiz, record, index) {
                    Ext.Ajax.request({
                        url: '/InfraApproveStep/Delete?Id=' + record.data.Id,
                        success: function (response, opts) {
                            var result = Ext.util.JSON.decode(response.responseText);
                            if (result.success == false) {
                                JsHelper.ShowError(result.msg);
                                this.insert(0, record);
                            }
                        }
                    });
                }
            }
        }, cfg));
    }
});

//InfraApproveStep GridPanel
Infrastructure.InfraApproveStep.GridPanel = Ext.extend(Infrastructure.BaseGridPanel, {
    ApproveId: null,
    ApproveName: null,
    // Property
    store: new Infrastructure.InfraApproveStep.JsonStore(),

    //initComponent
    initComponent: function () {

        var itemGrid = this;
        var cmdAddEntity = this.onAddEntity;

        // selModel
        var selModel = new Ext.grid.CheckboxSelectionModel({
            header: '',
            singleSelect: true,
            listeners: {
                'selectionchange': function (sm) {
                    // 控制工具栏按钮是否可用
                    itemGrid.delBtn.setDisabled(sm.getCount() < 1);
                }
            }
        });

        // columnModel 对于不常需要显示的 hidden: true 
        var gridColumn = new Ext.grid.ColumnModel({
            columns: [
				new Ext.grid.RowNumberer(),
				selModel,
				{ header: "步骤", dataIndex: 'ApproveStep' },
				{ header: "权重", dataIndex: 'Scale' },
                { header: "审批人", dataIndex: 'User.Name' },
                { header: "短信告警时间", dataIndex: 'SmsStartTime' },
				{ header: "短信告警间隔", dataIndex: 'SmsWarnTime' },
                { header: "描述", dataIndex: 'Description', hidden: true }
			],
            defaults: {
                sortable: false,
                menuDisabled: true
            }
        });

        // PageBar
        var pagebBar = new Infrastructure.BasePagingToolbar({
            store: this.store,
            plugins: this.plugins
        });

        // 常用工具栏
        var toolBar = [{
            xtype: 'tbaraddbtn',
            handler: cmdAddEntity,
            disabled: true,
            scope: itemGrid
        }, '-', {
            xtype: 'tbardelbtn',
            handler: function () {
                itemGrid.delEntity();
            },
            scope: itemGrid
        }];

        Ext.apply(this, {
            sm: selModel,
            cm: gridColumn,
            tbar: toolBar,
            bbar: pagebBar,
            listeners: {
                'rowdblclick': function (grid, rowIndex, e) {
                    this.onLoadEntity(grid.store.getAt(rowIndex), true);
                }
            }
        });

        Infrastructure.InfraApproveStep.GridPanel.superclass.initComponent.call(this);
    },

    /****************** 
    * user function 
    ******************/

    // 添加
    onAddEntity: function () {
        var itemGrid = this;
        var itemEditForm = new Infrastructure.InfraApproveStep.EditForm({
            ApproveId: this.ApproveId,
            ApproveName: this.ApproveName,
            listeners: {
                'afterSave': function (ele, rt) {
                    if (rt) {
                        itemGrid.store.reload();
                    }
                }
            }
        });
        var itemEditWin = new Infrastructure.InfraApproveStep.EditWin({
            items: [itemEditForm]
        });
        itemEditWin.show();
    },

    // 读取信息显示
    onLoadEntity: function (selRecord, isEdit) {
        var itemGrid = this;
        var selId = selRecord.data.Id;
        var itemEditForm = new Infrastructure.InfraApproveStep.EditForm({
            itemId: selId,
            dataRecord: selRecord,
            waitMsg: '数据加载中...',
            listeners: {
                'afterSave': function (ele, rt) {
                    if (rt) {
                        itemGrid.store.reload();
                    }
                }
            }
        });
        var itemEditWin = new Infrastructure.InfraApproveStep.EditWin({
            items: [itemEditForm]
        });
        itemEditWin.show();
    }
});

//InfraApproveStep EditForm
Infrastructure.InfraApproveStep.EditForm = Ext.extend(Infrastructure.BaseForm, {
    isEdit: true, //是否允许编辑
    submitUrl: '/InfraApproveStep/SaveOrUpdate',
    //prop
    ApproveId: null,
    ApproveName: null,


    initComponent: function () {
        var itemEditForm = this;
        if (this.dataRecord != null)
            this.dataId = this.dataRecord.data.Id;
        if (this.ApproveId != null)
            this.submitUrl = this.submitUrl + "?ApproveId=" + this.ApproveId;

        //Form第一列
        // 定义表单列
        var firstColumn = [{
            fieldLabel: "步骤",
            xtype: 'numberfield',
            name: 'ApproveStep'
        }, {
            fieldLabel: "权重",
            xtype: 'numberfield',
            name: 'Scale'
        }, {
            fieldLabel: "审批人",
            xtype: 'usercombo',
            hiddenName: 'UserId'
        }];

        var secondColumn = [{
            fieldLabel: "短信告警时间",
            xtype: 'numberfield',
            name: 'SmsStartTime'
        }, {
            fieldLabel: "短信告警间隔",
            xtype: 'numberfield',
            name: 'SmsWarnTime'
        }];

        var singleColumn = [{
            fieldLabel: "描述",
            width: 350,
            height: 50,
            maxLength: 255,
            xtype: 'textarea',
            name: 'Description'
        }];

        Ext.applyIf(this, {
            items: [{
                layout: 'column',
                // 注意 现在使用的是两列 要是三列或更多
                // 请改变下面的items
                // 并在上面定义每个列的组件集合
                items: [{
                    columnWidth: .5,
                    bodyStyle: 'padding:1px',
                    layout: 'form',
                    defaultType: 'textfield',
                    defaults: {
                        width: 150,
                        allowBlank: false,
                        readOnly: !this.isEdit
                    },
                    items: firstColumn
                }, {
                    columnWidth: .5,
                    bodyStyle: 'padding:1px',
                    layout: 'form',
                    defaultType: 'textfield',
                    defaults: {
                        width: 150,
                        allowBlank: false,
                        readOnly: !this.isEdit
                    },
                    items: secondColumn
                }]
            }, singleColumn],
            buttons: [{
                xtype: 'btnsubmit',
                handler: function () {
                    itemEditForm.saveItem(itemEditForm.submitUrl)
                },
                scope: itemEditForm
            }, {
                xtype: 'btncancle',
                handler: function () {
                    itemEditForm.ownerCt.destroy();
                }
            }],
            reader: new Infrastructure.InfraApproveStep.JsonReader()
        });

        Infrastructure.InfraApproveStep.EditForm.superclass.initComponent.call(this);

        //定义保存后执行的事件
        this.addEvents('afterSave');

        if (this.dataRecord != null) {
            this.form.loadRecord(this.dataRecord);
        }
    }
});

//InfraApproveStep DetailWin
Infrastructure.InfraApproveStep.EditWin = Ext.extend(Infrastructure.BaseWindow, {
    title: '审批步骤设置',
    iconCls: 'building_key',
    width: 600
});
